package top.lyc829.zzl_android.ui.index.serach;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.xuexiang.xui.widget.actionbar.TitleBar;
import com.xuexiang.xui.widget.flowlayout.FlowTagLayout;
import com.xuexiang.xui.widget.spinner.materialspinner.MaterialSpinner;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import okhttp3.Response;
import top.lyc829.zzl_android.R;
import top.lyc829.zzl_android.logic.bean.ResultBean;
import top.lyc829.zzl_android.logic.bean.SearchBean;
import top.lyc829.zzl_android.logic.bean.SearchData;
import top.lyc829.zzl_android.logic.network.Network;
import top.lyc829.zzl_android.logic.util.HttpUtil;
import top.lyc829.zzl_android.logic.util.Utils;
import top.lyc829.zzl_android.logic.util.gson.JsonUtilNew;

public class SearchActivity extends AppCompatActivity {
    FlowTagLayout mNormalFlowTagLayout;

    private SearchViewModel searchViewModel;


    private List<ImageView> searchLogoLists;
    private List<Button> searchButtonLists;
    private List<ImageButton> searchImageButtonLists;
    private List<SearchBean> searchHistory;
    private MaterialSpinner materialSpinner;
    private EditText editText;


    private void getLayoutItem() {
        searchLogoLists = new ArrayList<>();
        searchLogoLists.add(findViewById(R.id.search_logo_1));
        searchLogoLists.add(findViewById(R.id.search_logo_2));
        searchLogoLists.add(findViewById(R.id.search_logo_3));
        searchLogoLists.add(findViewById(R.id.search_logo_4));
        searchLogoLists.add(findViewById(R.id.search_logo_5));
        searchLogoLists.add(findViewById(R.id.search_logo_6));
        searchLogoLists.add(findViewById(R.id.search_logo_7));

        searchButtonLists = new ArrayList<>();
        searchButtonLists.add(findViewById(R.id.search_button_1));
        searchButtonLists.add(findViewById(R.id.search_button_2));
        searchButtonLists.add(findViewById(R.id.search_button_3));
        searchButtonLists.add(findViewById(R.id.search_button_4));
        searchButtonLists.add(findViewById(R.id.search_button_5));
        searchButtonLists.add(findViewById(R.id.search_button_6));
        searchButtonLists.add(findViewById(R.id.search_button_7));

        searchImageButtonLists = new ArrayList<>();
        searchImageButtonLists.add(findViewById(R.id.search_erase_1));
        searchImageButtonLists.add(findViewById(R.id.search_erase_2));
        searchImageButtonLists.add(findViewById(R.id.search_erase_3));
        searchImageButtonLists.add(findViewById(R.id.search_erase_4));
        searchImageButtonLists.add(findViewById(R.id.search_erase_5));
        searchImageButtonLists.add(findViewById(R.id.search_erase_6));
        searchImageButtonLists.add(findViewById(R.id.search_erase_7));
    }

    private void search() {
        SearchData searchData = new SearchData();
        if (Utils.isStringEmpty(editText.getText().toString())) {
            Utils.showToast("请输入关键词");
            return;
        }
        searchData.setString(editText.getText().toString());
        searchData.setType(materialSpinner.getSelectedIndex());
        Intent intent = new Intent(this, SearchDetial.class);
        intent.putExtra("searchData", searchData);
        startActivity(intent);
    }

    private void onSelectItemChange(int index) {
        List<SearchBean> curSearchHistory = new ArrayList<>();
        if (searchHistory != null)
            for (SearchBean bean : searchHistory)
                if (bean.getSearchType() == index) curSearchHistory.add(bean);
        Collections.reverse(curSearchHistory);
        for (int i = 0; i < curSearchHistory.size() && i < 7; ++i) {
            searchLogoLists.get(i).setVisibility(View.VISIBLE);
            searchButtonLists.get(i).setVisibility(View.VISIBLE);
            searchImageButtonLists.get(i).setVisibility(View.VISIBLE);

            searchButtonLists.get(i).setText(curSearchHistory.get(i).getString());
        }
        for (int i = curSearchHistory.size(); i < 7; ++i) {
            searchLogoLists.get(i).setVisibility(View.INVISIBLE);
            searchButtonLists.get(i).setVisibility(View.INVISIBLE);
            searchImageButtonLists.get(i).setVisibility(View.INVISIBLE);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        getLayoutItem();

        ImageButton searchButton = findViewById(R.id.imageButton);
        editText = findViewById(R.id.editText);
        ((TitleBar) findViewById(R.id.m_title_bar_search)).setLeftClickListener(v -> {
            finish();
        });

        materialSpinner = findViewById(R.id.add_search_type);

        materialSpinner.setOnItemSelectedListener((view, position, id, item) -> searchViewModel.getSearchLiveDataServer(materialSpinner.getSelectedIndex()));

        searchViewModel = new ViewModelProvider(this).get(SearchViewModel.class);
        searchViewModel.getSearchLiveDataServer(materialSpinner.getSelectedIndex());
        searchViewModel.getSearchLiveLiveData().observe(this, new Observer<List<SearchBean>>() {
            @Override
            public void onChanged(List<SearchBean> searchBeans) {
                searchHistory = searchBeans;
                onSelectItemChange(materialSpinner.getSelectedIndex());
            }

        });

        searchButton.setOnClickListener(
                v -> search());


        searchButtonLists.get(0).setOnClickListener(v -> {
            editText.setText(searchViewModel.getSearchLiveLiveData().getValue().get(0).getString());
            search();
        });


        searchButtonLists.get(1).setOnClickListener(v -> {
            editText.setText(searchViewModel.getSearchLiveLiveData().getValue().get(1).getString());
            search();
        });

        searchButtonLists.get(2).setOnClickListener(v -> {
            editText.setText(searchViewModel.getSearchLiveLiveData().getValue().get(2).getString());
            search();
        });

        searchButtonLists.get(3).setOnClickListener(v -> {
            editText.setText(searchViewModel.getSearchLiveLiveData().getValue().get(3).getString());
            search();
        });

        searchButtonLists.get(4).setOnClickListener(v -> {
            editText.setText(searchViewModel.getSearchLiveLiveData().getValue().get(4).getString());
            search();
        });

        searchButtonLists.get(5).setOnClickListener(v -> {
            editText.setText(searchViewModel.getSearchLiveLiveData().getValue().get(5).getString());
            search();
        });

        searchButtonLists.get(6).setOnClickListener(v -> {
            editText.setText(searchViewModel.getSearchLiveLiveData().getValue().get(6).getString());
            search();
        });

        searchImageButtonLists.get(0).setOnClickListener(v -> eraseSearchLiveDataServer(searchViewModel.getSearchLiveLiveData().getValue().get(0).getId()));

        searchImageButtonLists.get(1).setOnClickListener(v -> eraseSearchLiveDataServer(searchViewModel.getSearchLiveLiveData().getValue().get(1).getId()));

        searchImageButtonLists.get(2).setOnClickListener(v -> eraseSearchLiveDataServer(searchViewModel.getSearchLiveLiveData().getValue().get(2).getId()));

        searchImageButtonLists.get(3).setOnClickListener(v -> eraseSearchLiveDataServer(searchViewModel.getSearchLiveLiveData().getValue().get(3).getId()));

        searchImageButtonLists.get(4).setOnClickListener(v -> eraseSearchLiveDataServer(searchViewModel.getSearchLiveLiveData().getValue().get(4).getId()));

        searchImageButtonLists.get(5).setOnClickListener(v -> eraseSearchLiveDataServer(searchViewModel.getSearchLiveLiveData().getValue().get(5).getId()));

        searchImageButtonLists.get(6).setOnClickListener(v -> eraseSearchLiveDataServer(searchViewModel.getSearchLiveLiveData().getValue().get(6).getId()));

    }

    /**
     * 删除历史搜索记录
     *
     * @param id 搜索记录id
     */
    public void eraseSearchLiveDataServer(int id) {

        Network.eraseSearchHistory(id, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                String resultStr = Objects.requireNonNull(res.body()).string();
//                ResultBean<Void> resultBean = JsonUtilNew.fromJsonObject(resultStr, Void.class);
                ResultBean<List> resultBean = JsonUtilNew.fromJsonObject(resultStr, List.class);
                if (resultBean.getCode().equals(200)) {
                    runOnUiThread(() -> {
                        Utils.showToast("delete");
                        searchViewModel.deleteItem(id);
                    });
                }

                if (!resultBean.getCode().equals(200)) {
                    failed(new IOException(resultBean.getMsg()));
                }
            }

            @Override
            public void failed(IOException e) {
                Utils.Sout(e.getMessage());
            }
        });


    }

    @Override
    protected void onResume() {
        super.onResume();
        searchViewModel.getSearchLiveDataServer(materialSpinner.getSelectedIndex());
    }
}